package com.oyjd.beauty.business.goodsType.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.oyjd.beauty.bean.po.GoodsType;
import com.oyjd.beauty.bean.vo.GoodsTypeVO;
import com.oyjd.beauty.fw.action.Page;
import com.oyjd.beauty.fw.dao.BaseDao;


/**
 * 
 * 商品分类数据处理层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class GoodsTypeDao extends BaseDao {
	/**
	 * 获取商品分类列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<GoodsTypeVO> getList(Page<GoodsTypeVO> page) {
		String hqlResult = "select new " + GoodsTypeVO.class.getName() + GoodsTypeVO.STRUCT;
		String hqlCount = "select count(goodsType)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(GoodsType.class.getName()).append(" goodsType");
		sb.append(" where goodsType.status=0");
		Map<String, String[]> paras = page.getParas();
		List<Object> ps = new ArrayList<Object>();
		for (String key : paras.keySet()) {
			if ("name".equals(key) || "description".equals(key)) {
				sb.append(" and goodsType.").append(key).append(" like ?");
				ps.add("%" + paras.get(key)[0] + "%");
			}if ("beginTime".equals(key)) {// 开始日期 时间 格式：2012-09-08
				sb.append(" and DATE_FORMAT(goodsType.createTime,'%Y-%m-%d')>=?");
				ps.add(paras.get(key)[0]);
			} else if ("endTime".equals(key)) {// 结束日期 时间 格式：2012-09-08
				sb.append(" and DATE_FORMAT(goodsType.createTime,'%Y-%m-%d')<=?");
				ps.add(paras.get(key)[0]);
			}else{
				sb.append(" and goodsType.").append(key).append(" = ?");
				ps.add(paras.get(key)[0]);
			}
		}
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}	
	
	/**
	 * 根据ID获取商品分类对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public GoodsTypeVO getGoodsTypeVO(String goodsTypeId) {
		GoodsTypeVO result = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(GoodsTypeVO.class.getName()).append(GoodsTypeVO.STRUCT);
		sb.append(" from ").append(GoodsType.class.getName()).append(" goodsType");
		sb.append(" where goodsType.goodsTypeId=?");
		Query q=this.getSession().createQuery(sb.toString());
		q.setParameter(0, goodsTypeId);
		List<GoodsTypeVO> tmpList = (List<GoodsTypeVO>) q.list();
		if (tmpList.size() > 0) {
			result = tmpList.get(0);
		}
		return result;
	}
	
	/**
	 * 删除商品分类
	 * 
	 * @param ids
	 *            多个ID
	 * @param user
	 *            当前用户
	 * @return
	 */
	public void remove(String[] ids, String user) {
		StringBuffer sb = new StringBuffer();
		sb.append("update ").append(GoodsType.class.getName()).append(" goodsType set goodsType.status=1, goodsType.lastEditor=?, goodsType.lastUpdateTime=? where");
		for (int i = 0; i < ids.length; i++) {
			sb.append(0 == i ? "" : " or");
			sb.append(" goodsType.goodsTypeId=?");
		}
		Query q = getSession().createQuery(sb.toString());
		q.setParameter(0, user);
		q.setParameter(1, new Date());
		for (int i = 0; i < ids.length; i++) {
			q.setParameter(i + 2, ids[i]);
		}
		q.executeUpdate();
	}
}
